/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : motor.hpp
  * Description        :
  ******************************************************************************
  * Function           :
  *
  ******************************************************************************
  * Author             : Zhiping Fu
  * Creation Date      : Oct 23, 2020
  ******************************************************************************
  */
/* USER CODE END Header */

#ifndef __MOTOR_HPP
#define __MOTOR_HPP

#include "system/system.hpp"
#include "userlib/userlib.hpp"


#ifdef __cplusplus
extern "C" {
#endif


#define PWM_OUT_NUM 8
#define INI_PWM 1500
#define Min_PWM_Out  800 //800    //us
#define Max_PWM_Out  2500//2200   //us

//#define RAD_TO_PWM 687.76f			//
//#define CS_LIMIT_MIN -175
//#define CS_LIMIT_MAX 175     //175对应20度(0.349rad)
#define CS_LIMIT_MIN 290
#define CS_LIMIT_MAX 500
#define PWM_MID_1 1500 //left
#define PWM_MID_2 1500 //back
#define PWM_MID_3 1500 //right
#define PWM_MID_4 1500 //yaw
//#define PWM_MID_3 1413
//#define PWM_MID_4 1425


#ifdef __cplusplus
}
#endif



#ifdef __cplusplus

using namespace std;



class MOTOR
{
public:
	MOTOR(){}
	MOTOR(TIM_TypeDef *ha,char *n) : htimA(ha),name(n){}
	MOTOR(TIM_TypeDef *ha,TIM_TypeDef *hb,char *n) : htimA(ha),htimB(hb),name(n){}
	~MOTOR(){}

	void motor_Init(void);
	void motor_run(void);

	uint32_t  Timer_Sp;
	uint32_t  Timer_Sp_Last;

private:
	TIM_TypeDef *htimA;
	TIM_TypeDef *htimB;
	char *name;
	sCNT  Filt;

	bool  Update;     //更新  --
	eSTA  Sta;        //状态  --
	eERR  Err;        //错误信息  --
	bool  UnLock;     //解锁
	uint8_t requestTelemetry;

	u16 PWM[PWM_OUT_NUM];
	u16 PWM_OBS[PWM_OUT_NUM];
	s16 PwmOff[PWM_OUT_NUM];
	double p_limits;//控制舵幅值（度）

	uint32_t  mainrotorstopcount;
	uint32_t  startTimer;			//计时器
	uint32_t  stopTimer;			//计时器
	uint32_t  executionTime_us;			//计时器

	uint32_t  startTimerLast;			//计时器
	uint32_t  cycleTime_us;

	RC_command_msg rcCommand;
	Control_output_msg control_output;
	Motor_msg motor_msg;
	flow_msg flow;
};

#endif


#endif


/************************ (C) COPYRIGHT Longmen Drone Team *****END OF FILE****/
